package com.detrum.sportcamera.runnable;

import android.util.Log;

import com.detrum.base.BaseStatic;
import com.detrum.sportcamera.finals.SportStatic;
import com.detrum.sportcamera.finals.NfsNetFinal;
import com.detrum.sportcamera.utils.NetUtils;
import com.detrum.utils.LogUtils;

import java.util.TimeZone;

/**
 * Created by abc on 2017/11/21.
 */

public class SendOrder {
    // 共用数据帧头部
    private static byte[] head = new byte[]{0x14, 0x0c, 0x0b, 0x0b};

    /**
     * 发送的登录指令
     * @return
     */
    public static byte[] getLogin(){
        Log.i("wifiand","send login");
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.LOGIN};
        byte[] data = new byte[12];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) camHead.length;
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 登出指令
     * @return
     */
    public static byte[] getLogout(){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.LOGOUT};
        byte[] data = new byte[12];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) camHead.length;
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 开始预览指令
     * @return
     */
    public static byte[] getStartRtp(){
        Log.i("SendData","send StartRtp is "+ BaseStatic.DHCP_LOCAL_IP);
        Log.i("SendData","send StartRtp is "+ SportStatic.RTP_PORT);
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.START_RTP};
        if(BaseStatic.DHCP_LOCAL_IP==null){
            return null;
        }
        byte[] msgData = NetUtils.getIpBytes(BaseStatic.DHCP_LOCAL_IP);
        byte[] data = new byte[18];
        System.arraycopy(head, 0, data, 0, head.length);
        if(msgData==null){
            return null;
        }
        data[5] = (byte) (camHead.length + msgData.length + 2);
        data[16] = (byte) ((SportStatic.RTP_PORT & 0xFF00) >> 8);
        data[17] = (byte) (SportStatic.RTP_PORT & 0x00FF);
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        System.arraycopy(msgData, 0, data, 12, msgData.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 停止预览指令
     * @return
     */
    public static byte[] getStopRtp(){
        Log.i("SendData","send StopRtp");
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.STOP_RTP};
        if(BaseStatic.DHCP_LOCAL_IP==null){
            return null;
        }
        byte[] msgData = NetUtils.getIpBytes(BaseStatic.DHCP_LOCAL_IP);
        byte[] data = new byte[18];
        System.arraycopy(head, 0, data, 0, head.length);
        if(msgData==null){
            return null;
        }
        data[5] = (byte) (camHead.length + msgData.length + 2);
        data[16] = (byte) ((SportStatic.RTP_PORT & 0xFF00) >> 8);
        data[17] = (byte) (SportStatic.RTP_PORT & 0x00FF);
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        System.arraycopy(msgData, 0, data, 12, msgData.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 开始拍照指令
     */
    public static byte[] getTakePhoto(){
        Log.i("SendData","send TakePhoto");
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.TAKE_PHOTO};
        byte[] data = new byte[12];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) camHead.length;
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 设定清晰度--编码速率
     * @param msgData
     * @return
     */
    public static byte[] setCodeRate(byte[] msgData){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.SET_RECORD_QUALITY};
        byte[] data = new byte[12 + msgData.length];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) (camHead.length + msgData.length);
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        System.arraycopy(msgData, 0, data, 12, msgData.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 获取清晰度--编码速率
     * @return
     */
    public static byte[] getCodeRate(){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.GET_RECORD_QUALITY};
        byte[] data = new byte[12];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) camHead.length;
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 设置白平衡模式指令
     * @param msgData 设定的分辨率
     * @return
     */
    public static byte[] setWbMode(byte[] msgData){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.SET_WB_MODE};
        byte[] data = new byte[12 + msgData.length];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) (camHead.length + msgData.length);
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        System.arraycopy(msgData, 0, data, 12, msgData.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 得到当前白平衡模式指令
     */
    public static byte[] getWbMode(){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.GET_WB_MODE};
        byte[] data = new byte[12];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) camHead.length;
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 曝光设置指令
     * @param msgData 设定的曝光度
     * @return
     */
    public static byte[] setExposureMode(byte[] msgData){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.SET_EXPOSURE_MODE};
        byte[] data = new byte[12 + msgData.length];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) (camHead.length + + msgData.length);
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        System.arraycopy(msgData, 0, data, 12, msgData.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 得到当前曝光设置指令
     */
    public static byte[] getExporsureMode(){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.GET_EXPOSURE_MODE};
        byte[] data = new byte[12];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) camHead.length;
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 录像开始指令
     * @return
     */
    public static byte[] startRecord(){
        Log.i("SendData","send startRecord");
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.START_RECORD};
        byte[] data = new byte[12];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) camHead.length;
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 停止录像指令
     */
    public static byte[] stopRecord(){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.STOP_RECORD};
        byte[] data = new byte[12];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) camHead.length;
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 获取设定录像分辨率的指令
     * @param msgData 设定的分辨率
     * @return
     */
    public static byte[]setRecordResolution(byte[] msgData){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.SET_RECORD_RESOLUTION};
        byte[] data = new byte[12 + msgData.length];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) (camHead.length  + msgData.length);
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        System.arraycopy(msgData, 0, data, 12, msgData.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 从设备获取当前录像分辨率的指令
     * @return
     */
    public static byte[] getRecordResolution(){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.GET_RECORD_RESOLUTION};
        byte[] data = new byte[12];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) camHead.length;
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 从设备获取当前录像方式--是否循环录像
     * @return
     */
    public static byte[] getRecordPolicy(){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.GET_RECORD_POLICY};
        byte[] data = new byte[12];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) camHead.length;
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 设置当前录像方式--是否循环录像
     * @return
     */
    public static byte[] setRecordPolicy(byte[] msgData){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.SET_RECORD_POLICY};
        byte[] data = new byte[12 + msgData.length];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) (camHead.length  + msgData.length);
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        System.arraycopy(msgData, 0, data, 12, msgData.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    public static byte[] getTVMode(){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, (byte) NfsNetFinal.D_CAM_GET_TV_MODE};
        byte[] data = new byte[12];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) camHead.length;
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    public static byte[] setTVMode(byte[] msgData){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, (byte) NfsNetFinal.D_CAM_SET_TV_MODE};
        byte[] data = new byte[12 + msgData.length];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) (camHead.length  + msgData.length);
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        System.arraycopy(msgData, 0, data, 12, msgData.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    public static byte[] getOSDMode(){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, (byte) NfsNetFinal.D_CAM_GET_OSD};
        byte[] data = new byte[12];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) camHead.length;
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    public static byte[] setOSDMode(byte[] msgData){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, (byte) NfsNetFinal.D_CAM_SET_OSD};
        byte[] data = new byte[12 + msgData.length];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) (camHead.length  + msgData.length);
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        System.arraycopy(msgData, 0, data, 12, msgData.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 从设备获取电量信息
     */
    public static byte[] getBattery(){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.BAT_REMAIN};
        byte[] data = new byte[12];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) camHead.length;
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 从设备获取sd卡
     */
    public static byte[] getSDInfo(){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, (byte)NfsNetFinal.GET_SD_INFO};
        byte[] data = new byte[12];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) camHead.length;
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

    /**
     * 连接时发送当前时间给设备
     * @return
     */
    public static byte[] getTime(){
        Log.i("SendData","send Time");
        byte[] camHead = new byte[]{0x70, 0x01, 0x00, NfsNetFinal.SETTIME};
        byte[] msgData = new byte[8];
        long time1 = System.currentTimeMillis();
        TimeZone tz = TimeZone.getDefault();
        int timezoneOffset = tz.getRawOffset();
        LogUtils.i("send Time =========offset============= "+timezoneOffset);
        long time = (time1+timezoneOffset)/1000;
        msgData[0] = (byte) ((time & 0xFF000000) >> 24);
        msgData[1] = (byte) ((time & 0x00FF0000) >> 16);
        msgData[2] = (byte) ((time & 0x0000FF00) >> 8);
        msgData[3] = (byte) ((time & 0x000000FF));
        byte[] data = new byte[12 + msgData.length];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) (camHead.length + msgData.length);
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        System.arraycopy(msgData, 0, data, 12, msgData.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum & 0xFF);
        return data;
    }

    /**
     * 校验
     * @param buf
     * @param length
     * @return
     */
    public static long checkSum(byte[] buf, int length) {
        int i = 0;

        long sum = 0;
        long data ;

        while (length > 1) {
            data = (((buf[i] << 8) & 0xFF00) | ((buf[i + 1]) & 0xFF));
            sum += data;
            if ((sum & 0xFFFF0000) > 0) {
                sum = sum & 0xFFFF;
                sum += 1;
            }

            i += 2;
            length -= 2;
        }

        if (length > 0) {
            sum += (buf[i] << 8 & 0xFF00);
            if ((sum & 0xFFFF0000) > 0) {
                sum = sum & 0xFFFF;
                sum += 1;
            }
        }

        sum = ~sum;
        sum = sum & 0xFFFF;
        return sum;
    }

    /**
     * 发送更新设备指令
     */
    public static byte[] upgrade(){
        byte[] camHead = new byte[]{0x70, 0x01, 0x00,(byte)NfsNetFinal.D_CAM_DO_UPGRADE};

        byte[] data = new byte[12];
        System.arraycopy(head, 0, data, 0, head.length);
        data[5] = (byte) camHead.length;
        System.arraycopy(camHead, 0, data, 8, camHead.length);
        long checkSum = checkSum(data, data.length);
        data[6] = (byte)(( checkSum >> 8 ) & 0xFF);
        data[7] = (byte)(checkSum  & 0xFF);
        return data;
    }

}
